package controller;

import service.GoodsService;
import service.impl.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/add")
public class GoodsAddController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String gname = req.getParameter("gname");
        String gprice = req.getParameter("gprice");
        String gnumber = req.getParameter("gnumber");
        GoodsService service = new GoodsServiceImpl();
        int goods = service.add(gname, gprice, gnumber);
        if (goods != 0) {
            req.setAttribute("GoodAdd", goods);
            req.getRequestDispatcher("/goods/addgoods.jsp").forward(req, resp);
        }
        req.setAttribute("addError", "添加失败");
        req.getRequestDispatcher("/admin/main.jsp").forward(req, resp);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
